package com.wuliu.system.service.impl;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuliu.common.exception.ServiceException;
import com.wuliu.common.utils.DateUtils;

import javax.annotation.Resource;

import com.wuliu.common.core.domain.entity.SysAppletUser;
import com.wuliu.common.utils.SecurityUtils;
import com.wuliu.common.utils.StringUtils;
import com.wuliu.system.domain.BasQrcode;
import com.wuliu.system.mapper.BasQrcodeMapper;
import com.wuliu.system.service.CommonService;
import com.wuliu.system.service.WeixinService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.SysAppletUserMapper;
import com.wuliu.system.service.ISysAppletUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 前端用户Service业务层处理
 *
 * @author realue
 * @date 2023-10-22
 */
@Slf4j
@Service
public class SysAppletUserServiceImpl extends ServiceImpl<SysAppletUserMapper, SysAppletUser> implements ISysAppletUserService {
    @Resource
    private SysAppletUserMapper sysAppletUserMapper;
    @Resource
    private CommonService commonService;

    /**
     * 查询前端用户
     *
     * @param userId 前端用户主键
     * @return 前端用户
     */
    @Override
    public SysAppletUser selectSysAppletUserByUserId(Long userId) {
        return sysAppletUserMapper.selectSysAppletUserByUserId(userId);
    }

    /**
     * 查询前端用户列表
     *
     * @param sysAppletUser 前端用户
     * @return 前端用户
     */
    @Override
    public List<SysAppletUser> selectSysAppletUserList(SysAppletUser sysAppletUser) {
        return sysAppletUserMapper.selectSysAppletUserList(sysAppletUser);
    }

    /**
     * 新增前端用户
     *
     * @param sysAppletUser 前端用户
     * @return 结果
     */
    @Override
    public int insertSysAppletUser(SysAppletUser sysAppletUser) {
        long count = sysAppletUserMapper.selectCount(new LambdaQueryWrapper<SysAppletUser>()
                .eq(SysAppletUser::getSysAccount, sysAppletUser.getSysAccount())
                .eq(SysAppletUser::getUserNo, sysAppletUser.getUserNo()));
        if (count > 0) {
            throw new ServiceException("用户编号【" + sysAppletUser.getUserName() + "】已存在");
        }
        sysAppletUser.setCreateTime(DateUtils.getNowDate());
        return sysAppletUserMapper.insertSysAppletUser(sysAppletUser);
    }

    /**
     * 修改前端用户
     *
     * @param sysAppletUser 前端用户
     * @return 结果
     */
    @Override
    public int updateSysAppletUser(SysAppletUser sysAppletUser) {
        sysAppletUser.setUpdateTime(DateUtils.getNowDate());
        return sysAppletUserMapper.updateSysAppletUser(sysAppletUser);
    }

    /**
     * 批量删除前端用户
     *
     * @param userIds 需要删除的前端用户主键
     * @return 结果
     */
    @Override
    public int deleteSysAppletUserByUserIds(Long[] userIds) {
        return sysAppletUserMapper.deleteSysAppletUserByUserIds(userIds);
    }

    /**
     * 删除前端用户信息
     *
     * @param userId 前端用户主键
     * @return 结果
     */
    @Override
    public int deleteSysAppletUserByUserId(Long userId) {
        return sysAppletUserMapper.deleteSysAppletUserByUserId(userId);
    }

    /**
     * 获取业务员下单二维码
     *
     * @param type 1业务员码 2站点码
     * @return
     */
    @Override
    public String getUserQrCode(Integer type) {
        SysAppletUser user = SecurityUtils.getAppletUser();
        return commonService.getQrCodeEx(user.getDeptId(), user.getUserId(), user.getSysAccount(), type);
    }

    /**
     * 查询业务员列表 app端
     * @param userNo
     * @param userName
     * @return
     */
    @Override
    public List<SysAppletUser> getAppUsers(String userNo, String userName) {
        SysAppletUser user = SecurityUtils.getAppletUser();
        QueryWrapper<SysAppletUser> wrapper = new QueryWrapper<>();
        wrapper.eq("sys_account", user.getSysAccount());
        if (StringUtils.isNotEmpty(userNo)) {
            wrapper.eq("user_no", userNo);
        }
        if (StringUtils.isNotEmpty(userName)) {
            wrapper.like("user_name", userName);
        }
        return null;
    }
}
